import { baseRoute, asyncRoutes } from '@/router';
import store from '@/store';
// import Layout from '@/layouts';
import MainLayout from '@/layouts/mainLayout';
import IframeLayout from '@/layouts/iframe';
// import { setCache } from '@/utils/session';
// import router, { resetRouter } from '@/router';
const state = {
  routes: []
};

const mutations = {
  SET_ROUTE(state, route) {
    state.routes = baseRoute.concat(route);
  }
};

const actions = {
  async getRoute({ commit }) {
    let accessedRoutes = [];
    accessedRoutes = asyncRoutes;
    // if (role == 'admin') {
    //   accessedRoutes = asyncRoutes;
    // } else {
    //   accessedRoutes = filterAsyncRoute(asyncRoutes, role);
    // }
    let menus = await store.dispatch('user/getRouters');
    let routers = filterAsyncRouter(menus);
    accessedRoutes[0].children = accessedRoutes[0].children.concat(routers)
    commit('SET_ROUTE', accessedRoutes);
    return accessedRoutes;
  }
};


// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  console.log(lastRouter)
  return asyncRouterMap.filter(route => {
    // if (type && route.children) {
    //   route.children = filterChildren(route.children)
    // }
    if (route.component) {
      // Layout ParentView 组件特殊处理
      if (route.component === 'Layout') {
        route.component = MainLayout
      } else if (route.component === 'EmptyLayout') {
        route.component = MainLayout
      } else if (route && route.isFrame && route.isFrame === '0') {
        // 处理内嵌网页
        route.meta.link = route.component
        route.component = IframeLayout
      } else {
        route.component = loadView(route.component)
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children, route, type)
    } else {
      if (lastRouter.path) {
        route.path = lastRouter.path + '/' + route.path
      }
      delete route['children']
      delete route['redirect']
    }
    // 给指定路由下添加子路由
    // addChildren(route)
    return true
  })
}
// function filterChildren(childrenMap, lastRouter = false) {
//   var children = []
//   childrenMap.forEach((el, index) => {
//     console.log(index)
//     if (el.children && el.children.length) {
//       // if (el.component === 'ParentView') {
//         el.children.forEach(c => {
//           c.path = el.path + '/' + c.path
//           if (c.children && c.children.length) {
//             children = children.concat(filterChildren(c.children, c))
//             return
//           }
//           children.push(c)
//         })
//         return
//       // }
//     }
//     if (lastRouter) {
//       el.path = lastRouter.path + '/' + el.path
//     }
//     children = children.concat(el)
//   })
//   return children
// }

// 路由懒加载
export const loadView = view => {
  return resolve => require([`@/views/${view}`], resolve)
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
